<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Playlet;
use App\Model\PlayletEpisodes;
use App\Model\PlayletTag;
use App\Model\Tag;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletService;
#[AutoController]
class PlayletController  extends BaseController
{

    #[Inject]
    private PlayletService $playletService;
    /**
     * 短剧列表
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->playletService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 短剧创建
     */
    public function Create(RequestInterface $request){

        $result = $this->playletService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 短剧删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $playletInfo = Playlet::query()->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletInfo->deleted_at = date('Y-m-d H:i:s');
            $playletInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 短剧更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletInfo = Playlet::query()->select(['id','name','cover','introduction','price','is_finish','status','rank','company_id'])->where('id', $id)->first();


            if(empty($playletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            if($request->getMethod() == 'GET'){

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$playletInfo);

            }else{
                $result = $this->playletService->createOrUpdate($request->post(),true,$playletInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }

    public function Episodes(int $id){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select('id')->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['playlet_number as value','playlet_number_name as label'])->where(['playlet_id' => $id])->get();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }

    }


    public function EpisodesList(int $id){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select('id')->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            $playletEpisodesInfo = PlayletEpisodes::query()->select(['playlet_number','playlet_number_name','play_url','cover'])->where(['playlet_id' => $id])->orderBy('playlet_number','asc')->get();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletEpisodesInfo);
        }
    }

    public function batchUpload(int $id,RequestInterface $request){

        $playletInfo = Playlet::query()->select(['id','cover'])->where('id', $id)->first();
        if (empty($playletInfo)) {
            return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
        }

        $result = $this->playletService->batchUpload($request->post(),$id,$playletInfo->cover);

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '上传成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }

    }

    /**
     * 短剧标签
     */
    public function Tag(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{

            $playletInfo = Playlet::query()->select(['id','name','cover','introduction','price','is_finish','status'])->where('id', $id)->first();


            if(empty($playletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            if($request->getMethod() == 'GET'){
                $tag_ids = PlayletTag::query()->where(['playlet_id' => $id])->pluck('tag_id');
                if(!empty($tag_ids)){
                    $tag_ids = Tag::query()->whereIn('id',$tag_ids)->where('parent_id','!=',0)->pluck('id')->toArray();
                }


                //获取一级标签
                $parentData = Tag::query()->where(['parent_id' => 0])->get();
                $tagRes = array();
                if($parentData->isNotEmpty()){
                    foreach ($parentData as $parentDatum){
                        $tagInfo = ['value' => $parentDatum->id,'label' => $parentDatum->name];
                        $children = Tag::query()->where(['parent_id' =>  $parentDatum->id])->select(['id as value','name as label'])->orderBy('sort','desc')->get()->toArray();
                        foreach ($children as &$child){
                            if(in_array($child['value'],$tag_ids)){
                                $child['selected'] = true;
                            }
                        }
                        $tagInfo['children'] = $children;
                        $tagRes[] = $tagInfo;
                    }
                }
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$tagRes);

            }else{
                $result = $this->playletService->updateTag($request->post(),$playletInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }
            }
        }
    }
}